layui.use(['form', 'table'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#tbl',
        url: ctx + '/api/orders',
        toolbar: '#toolbar',
        cols: [[
            {type: "checkbox", width: 50},
            {field: 'longId', width: 200, title: '订单编号', sort: true},
            {
                field: 'user', width: 120, title: '买家名', sort: true, templet: function (d) {
                    return d.user.nickName || d.user.username;
                }
            },
            {field: 'createTime', width: 200, title: '下单时间'},
            {field: 'receiverAddr', width: 300, title: '收货地址'},
            {field: 'subTotal', width: 140, title: '订单总额(元)'},
            {field: 'tax', width: 140, title: '税收(元)'},
            {field: 'shipping', width: 120, title: '邮费(元)'},
            {field: 'payType', width: 100, title: '支付方式'},
            {field: 'state', width: 100, title: '订单状态'},
            {field: 'description', width: 100, title: '备注'},
            {title: '操作', minWidth: 240, toolbar: '#row-toolbar', align: "center"}
        ]],
        limits: [10, 15, 30, 50, 100],
        limit: 15,
        page: true
    });

    //监听搜索操作
    form.on('submit(search-btn)', function (data) {
        //表格重载
        table.reload('tbl', {
            page: {
                curr: 1
            }, where: data.field
        }, 'data');

        return false;
    });

    //重置
    $("#reset-btn").click(function () {
        $("#search-form")[0].reset();
    });

    /**
     * 表格toolbar监听事件
     */
    table.on('toolbar(tbl)', function (obj) {
        if (obj.event === 'send') {  // 监听批量发货操作
            let checkStatus = table.checkStatus('tbl')
                , data = checkStatus.data;

            let ids = [];
            data.forEach(t => ids.push(t.longId));

            if (ids.length === 0) {
                layer.msg("请选中您要发货的记录");
            } else {
                layer.confirm("是否确认对选中的记录发货", function (index) {
                    console.log(ids)
                    sendByIds(ids,()=>{
                        table.reload("tbl");
                    });

                    layer.close(index);
                });
            }
        } else if (obj.event === 'delete') {  // 监听批量删除操作
            let checkStatus = table.checkStatus('tbl')
                , data = checkStatus.data;

            let ids = [];
            data.forEach(t => ids.push(t.longId));

            if (ids.length === 0) {
                layer.msg("请选中您要删除的记录");
            } else {
                layer.confirm("是否确认删除选中的记录", function (index) {
                    deleteByIds(ids);
                    table.reload("tbl");
                    layer.close(index);
                });
            }
        }
    });

    //监听表格行工具栏
    table.on('tool(tbl)', function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
            layer.open({
                title: '编辑商品',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: ctx + '/good/edit?id=' + data.longId,
            });
            return false;
        } else if (obj.event === 'send') {//对选中行发货
            layer.confirm('是否确认发货？', function (index) {
                let ids = [obj.data.longId];
                sendByIds(ids, () => {
                    table.reload("tbl");
                });

                layer.close(index);
            });
        } else if (obj.event === 'delete') {
            layer.confirm('是否确认删除？', function (index) {
                let ids = [obj.data.longId];
                deleteByIds(ids);

                table.reload("tbl");

                layer.close(index);
            });
        }
    });


    function deleteByIds(ids) {
        let url = ctx + "/api/goods"
        $.ajax({
            url,
            method: "delete",
            dataType: "json",
            data: {
                ids
            },
            traditional: true,
            success(resp) {
                if (resp.success) {
                    layer.msg("成功标记删除选中的记录，共标记删除" + resp.rows + "条");
                } else {
                    layer.alert(resp.error);
                }
            }
        });
    }

    //发货，批量发货操作
    function sendByIds(ids, cb) {
        let url = ctx + "/api/orders/send"
        $.ajax({
            url,
            method: "patch",
            dataType: "json",
            data: {
                ids
            },
            traditional: true,
            success(resp) {
                if (resp.success) {
                    layer.msg("操作成功");
                    cb();
                } else {
                    layer.alert(resp.error);
                }
            }
        });
    }
});